<?php

$sqlF = sprintf("SELECT script FROM Page_mount WHERE Tabela='%s' GROUP BY script ORDER BY script", $TABLE);
$resultF = mysql_query($sqlF, $dbctl);
if($resultF == 0)
{
     printf("<h1>(PrepFunc-1)Falha: %s</h1>\n", mysql_error($dbctl));
     printf("<h3>%s</h3>\n", $sql);
     exit;
}
else
{
     if(mysql_num_rows($resultF) > 0)
     {
          while($myrowF = mysql_fetch_row($resultF))
          {
               // Arquivo com as Fun��es
               $inc = sprintf("<?php\n");
               $inc .= sprintf("function getDIR(\$value)\n");
               $inc .= sprintf("{\n");
               $inc .= sprintf("     \$ret = \"\";\n");
               $inc .= sprintf("     \$last = explode(\"/\", \$value);\n");
               $inc .= sprintf("     \$nome = explode(\".\", \$last[count(\$last) - 1]);\n");
               $inc .= sprintf("     if(substr(strtolower(\$nome[0]), 0, 3) == 'cad')\n");
               $inc .= sprintf("     {\n");
               $inc .= sprintf("          \$ret = strtoupper(substr(\$nome[0], 3, strlen(\$nome[0]) - 3));\n");
               $inc .= sprintf("     }\n");
               $inc .= sprintf("     else\n");
               $inc .= sprintf("     {\n");
               $inc .= sprintf("          \$ret = strtoupper(\$nome[0]);\n");
               $inc .= sprintf("     }\n");
               $inc .= sprintf("     return \"DIR_EDITAR_\".\$ret;\n");
               $inc .= sprintf("}\n");

               $sql = sprintf("SELECT campo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha = '0' ORDER BY LINHA,Coluna", $TABLE,$myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepFunc-2)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              $inc .= sprintf("function GetAnt(\$db,\$%s)\n", strtolower($myrow[0]));
                              $inc .= sprintf("{\n");
                              $inc .= sprintf("     \$ret = \"\$%s\";\n", strtolower($myrow[0]));
                              $inc .= sprintf("     \$sql = \"SELECT %s FROM %s WHERE %s < '\$%s' ORDER BY %s DESC LIMIT 1\";\n", strtolower($myrow[0]), ucfirst($TABLE), strtolower($myrow[0]), strtolower($myrow[0]), strtolower($myrow[0]));
                              $inc .= sprintf("     \$result = mysql_query(\$sql, \$db);\n");
                              $inc .= sprintf("     if(\$result == 0)\n");
                              $inc .= sprintf("     {\n");
                              $inc .= sprintf("          printf(\"Falha: %%s\\n\", mysql_error(\$db));\n");
                              $inc .= sprintf("     }\n");
                              $inc .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                              $inc .= sprintf("     {\n");
                              $inc .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                              $inc .= sprintf("          {\n");
                              $inc .= sprintf("               \$ret = \$myrow[0];\n");
                              $inc .= sprintf("          }\n");
                              $inc .= sprintf("     }\n");
                              $inc .= sprintf("     return \$ret;\n");
                              $inc .= sprintf("}\n");

                              $inc .= sprintf("function GetPos(\$db,\$%s)\n", strtolower($myrow[0]));
                              $inc .= sprintf("{\n");
                              $inc .= sprintf("     \$ret = \"\$%s\";\n", strtolower($myrow[0]));
                              $inc .= sprintf("     \$sql = \"SELECT %s FROM %s WHERE %s > '\$%s' ORDER BY %s LIMIT 1\";\n", strtolower($myrow[0]), ucfirst($TABLE), strtolower($myrow[0]), strtolower($myrow[0]), strtolower($myrow[0]));
                              $inc .= sprintf("     \$result = mysql_query(\$sql, \$db);\n");
                              $inc .= sprintf("     if(\$result == 0)\n");
                              $inc .= sprintf("     {\n");
                              $inc .= sprintf("          printf(\"Falha: %%s\\n\", mysql_error(\$db));\n");
                              $inc .= sprintf("     }\n");
                              $inc .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                              $inc .= sprintf("     {\n");
                              $inc .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                              $inc .= sprintf("          {\n");
                              $inc .= sprintf("               \$ret = \$myrow[0];\n");
                              $inc .= sprintf("          }\n");
                              $inc .= sprintf("     }\n");
                              $inc .= sprintf("     return \$ret;\n");
                              $inc .= sprintf("}\n");
                         }
                    }
               }

               $sql = sprintf("SELECT campo FROM Page_mount WHERE Tabela='%s' AND script='%s' AND Linha > '0' ORDER BY LINHA,Coluna", $TABLE, $myrowF[0]);
               $result = mysql_query($sql, $dbctl);
               if($result == 0)
               {
                    printf("<h1>(PrepFunc-3)Falha: %s</h1>\n", mysql_error($dbctl));
                    printf("<h3>%s</h3>\n", $sql);
                    exit;
               }
               else
               {
                    if(mysql_num_rows($result) > 0)
                    {
                         while($myrow = mysql_fetch_row($result))
                         {
                              if(strtolower($myrow[0]) == "foto" || strtolower($myrow[0]) == "imagem")
                              {
                                   $inc .= sprintf("function set%s(\$db, \$CODIGO)\n", ucfirst($myrow[0]));
                                   $inc .= sprintf("{\n");
                                   $inc .= sprintf("     if(isset(\$_FILES[%s]))\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("     {\n");
                                   $inc .= sprintf("          \$caminho = \"\";\n");

                                   $inc .= sprintf("          if(is_file(\$_FILES[%s][tmp_name]))\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("          {\n");
                                   $inc .= sprintf("               \$%s = get%s(\$db, \$CODIGO);\n", strtolower($myrow[0]), ucfirst($myrow[0]));
                                   $inc .= sprintf("               if(\$%s != \"\")\n", strtolower($myrow[0]));
                                   $inc .= sprintf("               {\n");
                                   $inc .= sprintf("                    \$caminho = \"./%s/\".\$%s;\n", ucfirst($myrow[0]), strtolower($myrow[0]));
                                   $inc .= sprintf("                    unlink(\$caminho);\n");
                                   $inc .= sprintf("               }\n");

                                   $inc .= sprintf("               \$arquivo = \$_FILES[%s][tmp_name];\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("               \$caminho = \"./%s/\";\n", ucfirst($myrow[0]));
                                   $inc .= sprintf("               list(\$nome, \$extensao) = explode(\".\", \$_FILES[%s][name]);\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("               \$caminho = \$caminho.\$CODIGO.\".\".\$extensao;\n");

                                   $inc .= sprintf("               if(!(eregi(\".php\$\", \$_FILES[%s][name])))\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("               {\n");
                                   $inc .= sprintf("                    copy(\$arquivo, \$caminho) or\n");
                                   $inc .= sprintf("                            die(\"<p>Erro durante a manipula&ccedil;&atilde;o do arquivo '\$arquivo'</p>\".'<p><a href=\"'.\$_SERVER['PHP_SELF'].'\">Voltar</a></p>');\n");
                                   $inc .= sprintf("               }\n");
                                   $inc .= sprintf("               \$caminho = \$CODIGO.\".\".\$extensao;\n");
                                   $inc .= sprintf("               \$sql = \"UPDATE %s SET %s='\".\$caminho.\"' WHERE idFuncionario='\".\$CODIGO.\"'\";\n", ucfirst($TABLE), ucfirst($myrow[0]));
                                   $inc .= sprintf("               \$result = mysql_query(\$sql, \$db);\n");
                                   $inc .= sprintf("          }\n");
                                   $inc .= sprintf("          if(isset(\$_REQUEST['remove_%s']))\n", strtoupper($myrow[0]));
                                   $inc .= sprintf("          {\n");
                                   $inc .= sprintf("               \$%s = get%s(\$db, \$CODIGO);\n", strtolower($myrow[0]), ucfirst($myrow[0]));
                                   $inc .= sprintf("               if(\$%s != \"\")\n", strtolower($myrow[0]));
                                   $inc .= sprintf("               {\n");
                                   $inc .= sprintf("                    \$caminho = \"./%s/\".\$%s;\n", ucfirst($myrow[0]), strtolower($myrow[0]));
                                   $inc .= sprintf("                    unlink(\$caminho);\n");
                                   $inc .= sprintf("               }\n");
                                   $inc .= sprintf("               \$sql = \"UPDATE %s SET %s='' WHERE id%s='\".\$CODIGO.\"'\";\n", ucfirst($TABLE), ucfirst($myrow[0]), ucfirst($TABLE));
                                   $inc .= sprintf("               \$result = mysql_query(\$sql, \$db);\n");
                                   $inc .= sprintf("          }\n");
                                   $inc .= sprintf("     }\n");
                                   $inc .= sprintf("}\n");
                              }

                              if(substr(strtolower($myrow[0]), 0, 2) == "id")
                              {
                                   $sel = ucfirst(substr($myrow[0], 2, strlen($myrow[0]) - 2));
                                   if(strstr($sel, "_id") != "")
                                   {
                                        $tb2 = strstr($sel, "_id");
                                        $sel = substr($sel, 0, strlen($sel) - strlen($tb2));
                                   }
                                   if(strstr($sel, "_") != "")
                                   {
                                        list($first, $second) = explode("_", $sel);
                                        $sel = sprintf("%s_%s", ucfirst($first), ucfirst($second));
                                   }
                                   if(is_numeric(substr($sel,strlen($sel)-1,1)))
                                   {
                                        $sel1 = substr($sel, 0, strlen($sel) - 1);
                                   }
                                   else
                                   {
                                        $sel1 = $sel;
                                   }
                                   $col = get2col($_REQUEST['Server'], $_REQUEST['User'], $_REQUEST['Pwd'], $_REQUEST['DB'], $sel);
                                   //$inc .= sprintf("function Sel%s(\$db,\$%s, \$ndx)\n", $sel, strtolower($myrow[0]));
                                   $inc .= sprintf("function Sel%s(\$db,\$ndx)\n", $sel);
                                   $inc .= sprintf("{\n");
                                   $inc .= sprintf("     \$sql = sprintf(\"SELECT id%s,%s FROM %s ORDER BY %s\");\n", strtolower($sel1), $col, $sel1, $col);
                                   $inc .= sprintf("     \$result = mysql_query(\$sql, \$db);\n");
                                   $inc .= sprintf("     if(\$result == 0)\n");
                                   $inc .= sprintf("     {\n");
                                   $inc .= sprintf("          printf(\"Falha: %%s\\n\", mysql_error(\$db));\n");
                                   $inc .= sprintf("     }\n");
                                   $inc .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                                   $inc .= sprintf("     {\n");
                                   $inc .= sprintf("          printf(\"<select name='%s' id='%s' tabindex='\$ndx'>\\n\");\n", strtoupper($myrow[0]), strtoupper($myrow[0]));
                                   $inc .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                                   $inc .= sprintf("          {\n");
                                   $inc .= sprintf("               printf(\"  <option value=\\\"\$myrow[0]\\\">\$myrow[1]</option>\\n\");\n");
                                   $inc .= sprintf("          }\n");
                                   $inc .= sprintf("          printf(\"</select>\\n\");\n");
                                   $inc .= sprintf("     }\n");
                                   $inc .= sprintf("}\n");

                                   $inc .= sprintf("function Get%s(\$db,\$id)\n", $sel);
                                   $inc .= sprintf("{\n");
                                   $inc .= sprintf("     \$ret = \"&nbsp;\";\n");
                                   $inc .= sprintf("     \$sql = sprintf(\"SELECT %s FROM %s WHERE %s='\$id'\");\n", $col, $sel1, strtolower($myrow[0]));
                                   $inc .= sprintf("     \$result = mysql_query(\$sql, \$db);\n");
                                   $inc .= sprintf("     if(\$result == 0)\n");
                                   $inc .= sprintf("     {\n");
                                   $inc .= sprintf("          printf(\"Falha: %%s\\n\", mysql_error(\$db));\n");
                                   $inc .= sprintf("     }\n");
                                   $inc .= sprintf("     if(mysql_num_rows(\$result) > 0)\n");
                                   $inc .= sprintf("     {\n");
                                   $inc .= sprintf("          while(\$myrow = mysql_fetch_row(\$result))\n");
                                   $inc .= sprintf("          {\n");
                                   $inc .= sprintf("               \$ret = \$myrow[0];\n");
                                   $inc .= sprintf("          }\n");
                                   $inc .= sprintf("     }\n");
                                   $inc .= sprintf("     return \$ret;\n");
                                   $inc .= sprintf("}\n");
                              }
                         }
                    }
               }
               $inc .= sprintf("?>\n");

               $dir = sprintf("./%s/my_inc", $diretorio);
               if(!is_dir($dir))
               {
                    mkdir($dir);
               }
               //$my_inc = sprintf("%s/Cad%s.inc.php", $dir, ucfirst($TABLE));
               $my_inc = sprintf("%s/Cad%s.inc.php", $dir, ucfirst($myrowF[0]));
               $fd = fopen($my_inc, "w");
               if($fd)
               {
                    fprintf($fd, "%s", $inc);
                    fclose($fd);
               }
          }
     }
}
?>
